package logic;
import gui.GuiMain;

import java.util.ArrayList;
import java.util.Stack;

import memory.GenericEvents;
import memory.Memory;

public class Delete {
	private static final String ITEM_NOT_FOUND = "Item not found: ";
	private static final String DELETED_ITEM = "Deleted item: ";
	private static final String IDENTICAL_NAME = "Multiple items with identical name found";
	private static final String DELETE_SUCCESS = "Deleted indexes.";
	private static final String NOT_IN_LISTVIEW = "Command available only in list view!";
	private static final String DISPLAY_ALL = "Display All";
	
	//@author A0096677H
	// Takes in ArrayList of sorted index to delete from memory
	public static void deleteAfterSearch(Stack<Integer> descendingList, Memory memory) {
		if (Display.listView != null) { // in list view
			while(!descendingList.isEmpty()){
				GenericEvents item = Display.listView.get(descendingList.pop()-1);
				deleteItem(item, memory);
				Display.listView.remove(item);
			}
			History.advance(memory);
			Display.refreshDisplay();

			System.out.println(DELETE_SUCCESS);
		} else { // not in list view
			assert Display.listView == null;
			System.out.println(NOT_IN_LISTVIEW);
		}

	}

	//@author A0096677H
	// Delete item currently shown in detailed view from memory
	public static void deleteDetailed(Memory memory) {
		assert memory != null;

		deleteItem(Display.detailedView, memory);
		History.advance(memory);
		Display.displayList(memory.getList(), DISPLAY_ALL);
	}

	//@author A0096677H
	// Perform a delete by name operation
	public static void deleteDirectly(String name, Memory memory) {
		ArrayList<GenericEvents> searchResult = Find.searchExact(name, memory);
		if (searchResult.size() == 1){
			deleteItem(searchResult.get(0), memory);
			History.advance(memory);
			Display.displayClear();
			System.out.println(DELETED_ITEM + name);
		} else if (searchResult.size() > 1) {
			Display.displayList(searchResult, IDENTICAL_NAME);
		}else {
			assert searchResult.size() < 1;
			System.out.println(ITEM_NOT_FOUND+ name);
		}
	}

	//@author A0096677H
	// Delete given GenericEvents item from memory
	private static void deleteItem(GenericEvents item, Memory memory) {
		assert item != null;
		GuiMain.words.remove(item.getName());
		memory.remove(item);
	}

}
